﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MS_UI
{
    public partial class FrmProtocolConfig : Form
    {
        public FrmProtocolConfig()
        {
            InitializeComponent();
            //初始化
            this.Load += FrmProtocolConfig_Load;

        }

        private void FrmProtocolConfig_Load(object sender, EventArgs e)
        {
            //初始化Modbus通讯信息
            //port

            for (int i = 0; i < 20; i++)
            { 
            
      this.cmb_port.Items.Add("COM"+i.ToString());
            }

            //每个给一个初始值
            this.cmb_port.SelectedIndex = 1;


            //Paud
            this.cmb_paud.Items.Add("4800");
            this.cmb_paud.Items.Add("9600");
            this.cmb_paud.Items.Add("14400");
            this.cmb_paud.Items.Add("19200");
            this.cmb_paud.Items.Add("38400");

            //每个给一个初始值
            this.cmb_paud.SelectedIndex = 1;



            //parit
            this.cmb_parity.Items.Add("None");
            this.cmb_parity.Items.Add("Odd");
            this.cmb_parity.Items.Add("Even");
            //Address
            for (int i = 0; i < 20; i++)
            {

                this.cmb_parity.Items.Add(i.ToString());

            }
            //每个给一个初始值
            this.cmb_parity.SelectedIndex = 1;




            //databits
            this.cmb_databits.Items.Add("7");
            this.cmb_databits.Items.Add("8");

            //每个给一个初始值
            this.cmb_databits.SelectedIndex = 1;


            //stopbits
            this.cmb_stopbits.Items.Add("1");
            this.cmb_stopbits.Items.Add("2");
            //每个给一个初始值
            this.cmb_stopbits.SelectedIndex = 1;


        }

        private string ModbusPath = Application.StartupPath + "\\ConfigFile\\" + "MODBUS.ini";


        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream fs;
                StreamWriter sw;
                //保存通讯协议,fileMode.create在这个是创建的意思，有了就覆盖没有就创建
                fs = new FileStream(ModbusPath, FileMode.Create);

                //写入初始化
                sw = new StreamWriter(fs,Encoding.Default);
                sw.WriteLine(this.cmb_port.Text.ToString());
                sw.WriteLine(this.cmb_address.Text.ToString());
                sw.WriteLine(this.cmb_paud.Text.ToString());
                sw.WriteLine(this.cmb_parity.Text.ToString());
                sw.WriteLine(this.cmb_databits.Text.ToString());
                sw.WriteLine(this.cmb_stopbits.Text.ToString());

                sw.Close();
                fs.Close();



            }
            catch (Exception ex)
            {
                MessageBox.Show("协议信息保存失败" + ex.ToString(), "保存提示");
                
            }

            MessageBox.Show("协议信息保存成功" , "保存提示");


        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
